<?php
// +----------------------------------------------------------------------
// | Name 风音框架
// +----------------------------------------------------------------------
// | Author 唐启云 <tqy@fxri.net>
// +----------------------------------------------------------------------
// | Copyright Copyright © 2016-2099 方弦研究所. All rights reserved.
// +----------------------------------------------------------------------
// | Link https://www.fxri.net
// +----------------------------------------------------------------------
namespace fxapp\facade_ext;

/**
 * 文本类
 */
class Text
{
    /**
     * 生成序列号
     * @param string $prefix 前缀
     * @return string
     */
    public function sn($prefix = '')
    {
        return strtoupper($prefix . time() . substr(microtime(), 2, 7) . rand(0, 9));
    }

    /**
     * 生成密码
     * @param array $param 参数
     * @param string $type 类型
     * @return string
     */
    public function password($param, $type = 'md5')
    {
        switch ($type) {
            default:
            case 'md5':
                $predefined = [
                    0, 1,
                ];
                $param = \fxapp\Param::define([$param, $predefined], '1.2.2');
                if (empty($param[1])) $param[1] = \fxapp\Math::rand(6);
                return md5(md5($param[0]) . $param[1]);
            case 'pbkdf2':
                return;
        }
    }

    /**
     * 扩展进制转换
     * @param mixed $var 变量
     * @param string $type 类型
     * @return mixed
     */
    public function convertExt($var, $type)
    {
        // 初始化变量
        $data = null;
        $type = strtolower($type);
        switch ($type) {
            case 'hex1':
                // 解析16进制，可见字符不解析
                $defs = [];
                $hexs = [];
                for ($i = 0; $i < strlen($var); $i++) {
                    $str = $var[$i];
                    $defs[$i] = $str;
                    if (dechex(hexdec($str)) != $str) {
                        $hexs[$i] = dechex(ord($str));
                        $defs[$i] = &$hexs[$i];
                    }
                }
                foreach ($hexs as $key => $value) {
                    if (strlen($value) == 1) {
                        $hexs[$key] = '0' . $value;
                    }
                }
                $hexs = $defs;
                $hexs = implode('-', $hexs);
                $hexs = strtoupper($hexs);
                $data = $hexs;
                break;
            case 'hex2':
                // 解析16进制，全文解析
                $hexs = [];
                for ($i = 0; $i < strlen($var); $i++) {
                    $str = $var[$i];
                    $hexs[$i] = dechex(ord($str));
                }
                foreach ($hexs as $key => $value) {
                    if (strlen($value) == 1) {
                        $hexs[$key] = '0' . $value;
                    }
                }
                $hexs = implode('-', $hexs);
                $hexs = strtoupper($hexs);
                $data = $hexs;
                break;
            case 'hex3':
                // 解析16进制，判断是否全部为可见字符
                $data = true;
                for ($i = 0; $i < strlen($var); $i++) {
                    $str = $var[$i];
                    $ord = ord($str);
                    if (!($ord > 31 && $ord < 127)) {
                        return false;
                    }
                }
                break;
        }
        return $data;
    }
}
